package com.hnd.springframework.beans.factory.support;

import com.hnd.springframework.beans.BeansException;
import com.hnd.springframework.beans.factory.BeanFactory;
import com.hnd.springframework.beans.factory.config.BeanDefinition;

/**
 * 抽象类
 *
 * @author hnd
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String beanName) throws BeansException {
        Object bean = getSingleton(beanName);
        if (bean != null) {
            return bean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(beanName);

        return createBean(beanName, beanDefinition);
    }

    /**
     * @param beanName
     * @return
     * @throws BeansException
     */
    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;

    /**
     * 创建bean实例
     *
     * @param beanName
     * @param beanDefinition
     * @throws BeansException
     */
    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException;
}
